﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CommandExecuter;
using Variables;

namespace CsharpGenerator
{
    class CsharpGenerator
    {
        private void GatherInfo()
        {
            
        }
        public void GenerateCode(string sourcepath, string exportpath)
        {
            StreamReader sr = new StreamReader(sourcepath);
            string fc = sr.ReadToEnd();
            sr.Close();
            string[] lines = fc.Split(new string[] {"\r\n"}, StringSplitOptions.None);
            this.WriteBaseUsing(exportpath);
            foreach (string line in lines)
            {
                ExecuteLine(line, exportpath);
            }
            this.WriteBottom(exportpath);
            
        }
        private void ExecuteLine(string line, string exportpath)
        {
            string[] spl = line.Split(' ');
            string tabs = "         ";
            string e = "\r\n";
            string nextargs = "";
            for(int i = 1;i<=spl.Length-1;i++)
            {
                if(nextargs=="")
                    nextargs = nextargs + spl[i];
                else
                    nextargs = nextargs + " " + spl[i];
            }
            using (StreamWriter sw = File.AppendText(exportpath))
            {
                switch (spl[0])
                {
                    case "display":
                        sw.WriteLine(tabs + "Console.Write(\"" +nextargs + "\");");
                        break;
                    case "NAMESPACE":
                        sw.WriteLine("namespace " + spl[1]);
                        sw.WriteLine("{");
                        break;
                    case "CLASS":
                        sw.WriteLine("  class " + spl[1]);
                        sw.WriteLine("  {");
                        break;
                    case "PROCEDURE":
                        sw.WriteLine("      public void " + spl[1]);
                        sw.WriteLine("      {");
                        break;
                    case "NSEND":
                        sw.WriteLine("}");
                        break;
                    case "CLEND":
                        sw.WriteLine("  }");
                        break;
                    case "PREND":
                        sw.WriteLine("      }");
                        break;

                }
                if (spl[0].StartsWith("#") && !spl[0].Contains("\\#"))
                {
                    string st = "";
                    spl[0].Remove(0,1);
                    for (int a = 1; a <= spl.Length - 1; a++)
                    {
                        if (st == "")
                            st = st + spl[a];
                        else
                            st = st + " " + spl[a];
                    }
                        sw.WriteLine("var " + spl[0] + " = " + st);
                }
                sw.Close();
            }
        }
        private void WriteBaseUsing(string exportpath)
        {
            using (StreamWriter sw = new StreamWriter(exportpath))
            {
                sw.WriteLine("using System;");
                sw.WriteLine("using System.Collections.Generic;");
                sw.WriteLine("using System.Linq;");
                sw.WriteLine("using System.Text;");
                sw.WriteLine("using System.Collections;");
                sw.WriteLine("using System.Net;");
                sw.WriteLine("using System.Net.Sockets;");
                sw.WriteLine("using System.IO;");
                sw.WriteLine("using System.Threading;");
                sw.WriteLine("using System.Threading.Tasks;");
                sw.WriteLine("using CommandExecuter;");
                sw.WriteLine("using Variables;");
                sw.WriteLine("using System.Data;");
                sw.WriteLine("using System.Drawing;");
                sw.WriteLine("");
                sw.Close();
            }
        }
        private void WriteConsHeader(string exportpath)
        {
            using (StreamWriter sw = new StreamWriter(exportpath))
            {
                sw.WriteLine("using System;");
                sw.WriteLine("using System.Collections.Generic;");
                sw.WriteLine("using System.Linq;");
                sw.WriteLine("using System.Text;");
                sw.WriteLine("using System.Collections;");
                sw.WriteLine("using System.Net;");
                sw.WriteLine("using System.Net.Sockets;");
                sw.WriteLine("using System.IO;");
                sw.WriteLine("using System.Threading;");
                sw.WriteLine("using System.Threading.Tasks;");
                sw.WriteLine("using CommandExecuter;");
                sw.WriteLine("using Variables;");
                sw.WriteLine("using System.Data;");
                sw.WriteLine("using System.Drawing;");
                sw.WriteLine("");
                sw.WriteLine("namespace Application");
                sw.WriteLine("{");
                sw.WriteLine("  class Application");
                sw.WriteLine("  {");
                sw.WriteLine("      public static void main(string[] args)");
                sw.WriteLine("      {");
                sw.Close();
            }
        }
        private void WriteBottom(string exportpath)
        {
            using (StreamWriter sw = File.AppendText(exportpath))
            {
                sw.WriteLine("      }");
                sw.WriteLine("  }");
                sw.WriteLine("}");
            }
        }
    }
}